<?php 


require "../common/Curl.php";

/**
 *	https://open.esign.cn/doc/detail?id=opendoc%2Fpaas_api%2Fsgv49u&namespace=opendoc%2Fpaas_api
 * 	电子签名SaaS API非标版
 * 	模板管理API----接口顺序排列封装
 *  使用请求签名鉴权方式
 *  下一个------------------ 签署服务 sign.php 
 */


// 获取文件contentMd5
function getContentBase64Md5($filePath){
    //获取文件MD5的128位二进制数组
    $md5file = md5_file($filePath,true);
    //计算文件的Content-MD5
    $contentBase64Md5 = base64_encode($md5file);
    return $contentBase64Md5;
}

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

// 模板文件上传两个步骤
// 步骤一 获取模板文件上传地址

function createByUploadUrl($contentMd5,$contentType,$fileName,$convert2Pdf,$appId,$secret,$url){
	$urlss = $url."/v1/docTemplates/createByUploadUrl";
	$head_url = "/v1/docTemplates/createByUploadUrl";
	$data = [
		'contentMd5'=>$contentMd5,
		"contentType"=>$contentType,
	    "convert2Pdf"=>$convert2Pdf,
	    "fileName"=>$fileName
	];
	$res = json_encode($data);
    $header = getHeader($data,"POST",$head_url,$appId,$secret,$url);
    $result = sendHttp($method="POST",$urlss,$header,$res);
    return $result;
}


// 步骤二 文件流上传
function fielUploadTemplate($filePath,$getUploadUrl,$contentMd5,$contentType){
	$urlss = $getUploadUrl;
	$res = file_get_contents($filePath);
	$header = array(
        'Content-MD5:'.$contentMd5,
        'Content-Type:'.$contentType
    );
    $result = sendHttp($method="PUT",$urlss,$header,$res);
    return $result;
}

// $filePath = "./wxhead.png";
// $contentMd5 = getContentBase64Md5($filePath);
// $contentType = "application/octet-stream";
// $convert2Pdf = false;
// $fileName = "wxhead.png";
// $a = createByUploadUrl($contentMd5,$contentType,$fileName,$convert2Pdf,$appId,$secret,$url);
// $b = json_decode($a,true);
// var_dump($a);
// var_dump($b['data']['uploadUrl']);


// $getUploadUrl = $b['data']['uploadUrl'];   // 获取生成的url
// $b = fielUploadTemplate($filePath,$getUploadUrl,$contentMd5,$contentType);
// var_dump($b);die;



//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/**
 * [selectTemplateFileStatus 查询m模板文件上传状态]
 * @param  [type] $templateId [模板文件id]
 * @return [json]         [模板文件上传状态。0-未上传 1-未转换成PDF 2-已上传成功 3-已转换成PDF]
 */
function selectTemplateFileStatus($templateId,$appId,$secret,$url){
	$urlss = $url."/v1/docTemplates/".$templateId."/getBaseInfo";
	$head_url = "/v1/docTemplates/".$templateId."/getBaseInfo";
	$data = [];
	$res = json_encode($data);
    $header = getHeader($data,"GET",$head_url,$appId,$secret,$url);
    $result = sendHttp($method="GET",$urlss,$header,$res);
    return $result;
}
// $templateId = "aacf725655f24c5598fc870799626769";
// $a = selectTemplateFileStatus($templateId,$appId,$secret,$url);
// var_dump($a);die;

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/**
 * [selectTemplateFile 获取模板文件详情]
 * @param  [type] $fileId [模板文件id]
 * @return [json]         [返回详情]
 */
function selectTemplateFile($fileId,$appId,$secret,$url){
	$urlss = $url."/v1/docTemplates/".$fileId;
	$head_url = "/v1/docTemplates/".$fileId;
	$data = [];
	$res = json_encode($data);
    $header = getHeader($data,"GET",$head_url,$appId,$secret,$url);
    $result = sendHttp($method="GET",$urlss,$header,$res);
    return $result;
}
// $fileId = "aacf725655f24c5598fc870799626769";
// $a = selectTemplateFile($fileId,$appId,$secret,$url);
// var_dump($a);die;

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/**
 * [createByTemplateContent 通过动态传入数据的方式填充模板文件内容并生成PDF文件]
 * @param  [type] $name             [文件名称]
 * @param  [type] $templateId       [模板编号]
 * @param  [type] $simpleFormFields [输入项填充内容，key:value 传入；可使用输入项组件id+填充内容，也可使用输入项组件key+填充内容方式填充]
 * @return [json]                   [返回下载链接结果]
 */
function createByTemplateContent($name,$templateId,$simpleFormFields,$appId,$secret,$url){
	$urlss = $url."/v1/files/createByTemplate";
	$head_url = "/v1/files/createByTemplate";
    $data = [
		"name"=>$name,
		"templateId"=>$templateId,
	    "simpleFormFields"=>$simpleFormFields
	];
    $res = json_encode($data);
    var_dump($res);
    $header = getHeader($data,"POST",$head_url,$appId,$secret,$url);
    $result = sendHttp($method="POST",$urlss,$header,$res);
    return $result;
}
// $name = 'wxhead.png';
// $templateId = "aacf725655f24c5598fc870799626769";
// $simpleFormFields = [
// 		"jiafang"=>"lsj",
// 		"yifang"=>"yifang"
// 	];
// $a = createByTemplateContent($name,$templateId,$simpleFormFields,$appId,$secret,$url);
// var_dump($a);die;

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/**
 * [selectPdfFileStatus 获取PDF文件详情]    这个和文件管理API查询PDF文件详情是一样的
 * @param  [type] $fileId [文件id]
 * @return [json]         [返回详情]
 */
function selectPdfFileStatus1($fileId,$appId,$secret,$url){
	$urlss = $url."/v1/files/".$fileId;
	$head_url = "/v1/files/".$fileId;
	$data = [];
	$res = json_encode($data);
    $header = getHeader($data,"GET",$head_url,$appId,$secret,$url);
    $result = sendHttp($method="GET",$urlss,$header,$res);
    return $result;
}
// $fileId = "aacf725655f24c5598fc870799626769";
// $a = selectPdfFileStatus1($fileId,$appId,$secret,$url);
// var_dump($a);die;

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/**
 * [createControl 添加填写控件 会有很多参数都要写，按官方表里写对应参数structComponent]
 * @param  [type] $templateId      [模板id]
 * @param  [type] $structComponent [各类参数]
 * @return [json]                  [返回结果]
 */
function createControl($templateId,$structComponent,$appId,$secret,$url){
	$urlss = $url."/v1/docTemplates/".$templateId."/components";
	$head_url = "/v1/docTemplates/".$templateId."/components";
    $data = [
	    "structComponent"=>$structComponent
	];
    $res = json_encode($data);
    $header = getHeader($data,"POST",$head_url,$appId,$secret,$url);
    $result = sendHttp($method="POST",$urlss,$header,$res);
    return $result;
}
// $templateId = "60d43e47cd89479480a001a91fb4ea0c";
// $structComponent = [
// 		[
// 			"id"=>"",
// 			"age"=>"21",
// 			"type"=>1,
// 			"context"=>
// 			[
// 				"label"=>"身份证",
// 				"age"=>"21",
// 				"style"=>
// 				[
// 					"font"=>1,
//                     "fontSize"=>11,
//                     "textColor"=>"#000000",
//                     "width"=>11.11,
//                     "height"=>11.11
// 				],
// 				"pos"=>
// 				[
// 					"x"=>11.11,
//                     "y"=>11.11,
//                     "page"=>1
// 				]
// 			]
// 		]
// 	];
// $a = createControl($templateId,$structComponent,$appId,$secret,$url);
// var_dump($a);die;    //12df6f96e28b4edda3607e82220cd2d7

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/**
 * [deleteControl 删除填写控件]
 * @param  [type] $templateId [模板id]
 * @param  [type] $ids        [输入项组件id集合，逗号分隔，]
 * @return [json]             [返回结果]
 */
function deleteControl($templateId,$ids,$appId,$secret,$url){
	$urlss = $url."/v1/docTemplates/".$templateId."/components/".$ids;
	$head_url = "/v1/docTemplates/".$templateId."/components/".$ids;
	$data =[];
    $res = json_encode($data);
    $header = getHeader($data,"DELETE",$head_url,$appId,$secret);
    $result = sendHttp($method="DELETE",$urlss,$header,$res);
    return $result;
}
// $templateId = "60d43e47cd89479480a001a91fb4ea0c";
// $ids = "123";
// $a = deleteControl($templateId,$ids,$appId,$secret,$url);
// var_dump($a);die;


